/**
 * 游戏商城数据类
 */
class gameShopData extends eui.Component {
	public constructor() {
		super();
		this.skinName = "resource/assets/skins/ShopMall.exml";
		st_config.setScreenAuto(this);
		this.init();

	}
	private static instance: gameShopData;
	/**获取单例对象 */
	public static getInstance(): gameShopData {
		if (this.instance == null) {
			this.instance = new gameShopData();
		}
		return this.instance;
	}
	//是否有试用
	public isHaveTry: boolean = false;
	/**提示框 */
	// private pifu_tan: eui.Group;

	/**提示图 */
	// private need_chengjiu: eui.Image;
	// private biankuang: eui.Image;
	// private wen: eui.Label;
	// /**10000蛙币 */
	// private gold: eui.Label;
	/**提示框 */
	public toolTip: eui.Group;
	/**提示框确定 */
	public Yes: eui.Image;
	public toolTipsBg: eui.Image;
	/**提示字 */
	public Tip: eui.Label;
	/**关闭按钮 */
	private closeShop: eui.Image;
	/**用于防止用户误操作 */
	private noClick: eui.Image;
	/**商品管理类 */
	public m_gameShopMgr: gameShopView = new gameShopView(this);

	/**显示商品类型按钮 */
	private ornamentButton: eui.Label;
	/**商城拖尾按钮 */
	//private trailingButton: eui.Label;
	/**分享按钮 */
	//private fenxiangBtn: eui.Button;
	/**用户蛙币 */
	public usermoney: eui.Label;
	public trynum: number;

	//private hallADR = "http://172.16.19.224:8800";

	/**商品层 */
	private shops: any;

	private init() {
		this.m_gameShopMgr.init();
		this.getUserMoney();
		this.noClick.touchEnabled = true;
		this.closeShop.touchEnabled = true;
		this.closeShop.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
			this.parent.removeChild(this);
			this.toolTip.visible = false;
			SoundManager.getInstance().playEffectSound("click_mp3");
		}, this);
		this.getUserAdress();
		this.ornamentButton.touchEnabled = true;
		this.ornamentButton.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
			this.changeDisplayShopType(0);
			SoundManager.getInstance().playEffectSound("click_mp3");
		}, this);

		/**点击商城拖尾 */
		//this.trailingButton.touchEnabled = true;
		// this.trailingButton.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
		// 	this.changeDisplayShopType(1);
		// 	SoundManager.getInstance().playEffectSound("click_mp3");
		// }, this);
		//this.fenxiangBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.fen, this)


	}

	// /**点击分享 */
	// public fen() {
	// 	SoundManager.getInstance().playEffectSound(true,"onClick_mp3");
	// 	console.log("点击分享");
	// 	weixinManager.getInstance().clikShare()
	// 	MiniNet.post(DataProl.shopaddr, '/shop_share', 'accountId=' + DataProl.account, this, this.getUserMoneyCallBack);
	// }

	/**获取玩家金币 */
	public getUserMoney() {
		MiniNet.post(DataProl.shopaddr, '/shop_GetUserMoney', 'accountId=' + DataProl.account, this, this.getUserMoneyCallBack);
		console.log(1525010063);
	}

	/**获取玩家金币回调 */
	private getUserMoneyCallBack(Data: any) {
		var userMoneyData = JSON.parse(Data.target.response);
		DataProl.userCurrentDressUp.userMoney = userMoneyData.usermoney;
		this.showMoney();
		console.log(userMoneyData.usermoney);

	}

	/**显示玩家金币 */
	public showMoney() {
		this.usermoney.text = DataProl.userCurrentDressUp.userMoney + "";
		console.log(DataProl.userCurrentDressUp.userMoney);
	}

	/**获取全部商品回调 */
	private allShopCallBack() {
		this.m_gameShopMgr.serverAllGoods(GlobalData.shopsData);
		this.getMineData();
		GlobalData.isGetShop = 1;
	}

    /**
	 * 向服务器获取玩家的背包
	 */
	private getMineData() {
		MiniNet.post(DataProl.shopaddr, '/shop_GetUserShop', 'accountId=' + DataProl.account, this, this.mineShopsBack);
	}
	private nowDisplayType = null;

	/**
	 * 获取背包回调
	 */
	private mineShopsBack(shopsData: any) {

		this.m_gameShopMgr.ownDataDispos(JSON.parse(shopsData.target.response));
		/**显示所有商品 */
		if (this.nowDisplayType == null)
			this.m_gameShopMgr.displayGoods(0);
		else {
			this.m_gameShopMgr.displayGoods(this.nowDisplayType);
			this.nowDisplayType = null;
		}
	}

	/**购买商品 */
	// private buyGoods(shopId, goodstype, zpiont) {
	// 	this.nowDisplayType = goodstype;
	// 	this.pifu_tan.visible = true;
	// 	this.pifu_tan.x = zpiont.x + 225;
	// 	this.pifu_tan.y = zpiont.y + 140;
	// 	this.biankuang.scaleY = 0;
	// 	this.biankuang.scaleX = 2;
	// 	this.need_chengjiu.visible = false;
	// 	this.wen.visible = false;
	// 	this.gold.visible = false;
	// 	egret.Tween.get(this.biankuang).to({ scaleX: 1, scaleY: 0.05 }, 200).to({ scaleY: 1 }, 100).call(() => {
	// 		this.need_chengjiu.visible = true;
	// 		this.wen.visible = true;
	// 		this.gold.visible = true;
	// 		this.pifu_tan.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
	// 			this.need_chengjiu.visible = false;
	// 			this.wen.visible = false;
	// 			this.gold.visible = false;
	// 			egret.Tween.get(this.biankuang).to({ scaleX: 1, scaleY: 0.05 }, 100).to({ scaleX: 2, scaleY: 0 }, 200).call(() => {
	// 				this.pifu_tan.visible = false;
	// 			})
	// 		}, this);
	// 	})
	// 	if (goodstype == 0) {
	// 		this.need_chengjiu.texture = RES.getRes("chengjiuxunzhang_" + (shopId % 10).toString());
	// 	} else {
	// 		this.need_chengjiu.texture = RES.getRes("chengjiuxunzhang_" + ((shopId % 10) + 5 > 7 ? 7 : (shopId % 10) + 5).toString());
	// 	}

	// 	console.log("///////////*********" + shopId % 10)
	// 	//this.shops.setChildIndex(this.pifu_tan,15);
	// 	console.log("//////////***********////" + goodstype + "///////////*********" + shopId)
	// 	MiniNet.post(DataProl.shopaddr, '/shop_addUserShop', 'accountId=' + DataProl.account + '&shopId=' + shopId, this, this.buyGoodsCallBack);
	// 	console.log(this.usermoney);

	// }

	/**购买商品回调 */
	private buyGoodsCallBack(data: any) {
		var buyData = JSON.parse(data.target.response);
		if (buyData.usermoney != null) {
			DataProl.userCurrentDressUp.userMoney = buyData.usermoney;
		}
		this.showMoney();
		/**购买成功 */
		if (buyData.isUpdata) {
			this.setActionByShopAction(buyData.goodsId, 1);
			this.getMineData();

		} else {
			this.toolTip.visible = true;
		}
	}

	private showToolTip() {
		console.log("sdaffa");
		this.toolTip.visible = true;
		this.Yes.visible = true;
		this.Tip.visible = true;
		this.toolTipsBg.visible = true;
		this.toolTip.touchEnabled = true;
		this.Yes.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
			this.toolTip.visible = false;
		}
			, this)
	}


	/**获取玩家当前装扮 */
	private getUserAdress() {
		MiniNet.post(DataProl.shopaddr, '/shop_getUserAdressUp', 'accountId=' + DataProl.account, this, this.userAdressCallBack);

	}
	public isTry: boolean = false;
	/**获取玩家装扮回调 */
	private userAdressCallBack(date: any) {
		var dressUp = JSON.parse(date.target.response);
		DataProl.userCurrentDressUp.decorateId = dressUp.dressData[0].DecorateId;
		DataProl.userCurrentDressUp.trailingId = dressUp.dressData[0].TrailingId;
		DataProl.userCurrentDressUp.userMoney = dressUp.dressData[0].userMoney;
		DataProl.userCurrentDressUp.sign = dressUp.dressData[0].sign;
		DataProl.try.decorateId = dressUp.dressData[0].DecorateId;
		DataProl.try.trailingId = dressUp.dressData[0].TrailingId;

	}

	/**操作商品  装备/卸下 */
	private operatingMerchandise(shopId: any) {
		this.initializeShop();
		/**是否穿戴当前商品 >=0为拥有 */
		if (shopId == DataProl.userCurrentDressUp.decorateId || shopId == DataProl.userCurrentDressUp.trailingId) {
			this.unloadDecoration(shopId);
		}
		else
			this.putOnTheDecoration(shopId);
	}
	/**初始化商店 */
	private initializeShop() {
		var shopsData = this.shops.$children;
		shopsData.forEach(e => {
			if (e.isHave == true) {
				e.changeShopAction(1);
			}
		})
	}

	/**穿上装饰 */
	public putOnTheDecoration(shopId: any) {
		// console.log("玩家穿上装饰 " + shopId);
		if (shopId >= 2000) {
			DataProl.userCurrentDressUp.trailingId = shopId;
		}
		else {
			/**大于0代表已经穿上装饰了 */
			DataProl.userCurrentDressUp.decorateId = shopId;
			console.log("测试中");
		}
		this.updataAdressUp();
		this.setActionByShopAction(shopId, 2);
		MiniNet.post(DataProl.shopaddr, '/shop_getUserAdressUp', 'accountId=' + DataProl.account, this, this.userAdressCallBack);
	}


	/**卸下装饰 */
	public unloadDecoration(shopId: any) {
		if (shopId >= 2000) {
			DataProl.userCurrentDressUp.trailingId = -1;
		}
		else {
			DataProl.userCurrentDressUp.decorateId = -1;
		}
		this.updataAdressUp();
		this.setActionByShopAction(shopId, 1);

	}

	/**更新玩家装扮 */
	private updataAdressUp() {
		var data = 'accountId=' + DataProl.account + '&decorateId=' + DataProl.userCurrentDressUp.decorateId + '&trailingId=' + DataProl.userCurrentDressUp.trailingId;
		MiniNet.post(DataProl.shopaddr, '/shop_updataAdressUp', data, this, this.buyGoodsCallBack);
		console.log("装扮更新中" + DataProl.userCurrentDressUp.decorateId);
	}


	/**根据商品动作下标设置商品动作 */
	private setActionByShopAction(shopId: number, ActionIndex: number) {
		if (shopId == null)
			return;
		var shopsData = this.shops.$children;
		shopsData.forEach(e => {
			if (e.name == shopId.toString()) {
				if (ActionIndex == 1)
					e.isHave = true;
				e.changeShopAction(ActionIndex);
			}
		})
	}

	/**切换显示商品类型 */
	public changeDisplayShopType(showType: number) {
		this.toolTip.visible = false;
		this.shops.removeChildren();
		this.ornamentButton.textColor = showType == 0 ? 0x8aff00 : 0xFFFFFF;
		//this.trailingButton.textColor = showType == 1 ? 0x8aff00 : 0xFFFFFF;
		this.m_gameShopMgr.displayGoods(showType);
	}

	/**分享成功 */
	public static shareSuccess() {
		var data = 'accountId=' + DataProl.account;
		MiniNet.post(DataProl.shopaddr, '/user_shareSuccess', data, this, null);
	}

}